﻿using Common;
using Entity;
using Police.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Police.Controllers
{
    public class SysController : Controller
    {
        private SysMenuBLL bSysMenu = new SysMenuBLL();
        private SysUserBLL bSysUser = new SysUserBLL();

        public ActionResult Menu()
        {
            return View();
        }

        public ActionResult TreeMenuEdit()
        {
            return View();
        }

        public ActionResult User()
        {
            return View();
        }

        [HttpPost]
        public ActionResult MenuTree()
        {
             List<TreeNode> nodes = bSysMenu.GetTree();
             return Json(nodes);
        }

        [HttpPost]
        public ActionResult GetMenu()
        {
            int id = int.Parse(Request.Form["id"].ToString());
            SysMenu model = bSysMenu.GetById(id);
            return Json(model);
        }

        [HttpPost]
        public ActionResult DeleteMenu()
        {
            string ids = Request.Form["ids"].ToString();

            string[] arr = ids.Split(',');

            foreach (string item in arr)
            {
                int id = int.Parse(item);
                SysMenu model = new SysMenu();
                model.MenuId = id;
                bSysMenu.Delete(model);
            }

            return Json("ok");
        }

        [HttpPost]
        public ActionResult EditMenu()
        {
            string json = Request.Form["json"].ToString();
            SysMenu model = new JavaScriptSerializer().Deserialize<SysMenu>(json);
            model.UpdateTime = DateTime.Now;
            if (model.MenuId != 0)
                bSysMenu.Update(model);
            else
            {
                model.CreateTime = DateTime.Now;
                model = bSysMenu.Add(model);
            }
            return Json(model);
        }

        [HttpPost]
        public ActionResult GetUser()
        {
            int pageIndex = int.Parse(Request.Form["pageIndex"].ToString());
            int pageSize = int.Parse(Request.Form["pageSize"].ToString());

            List<SysUser> models = bSysUser.GetList(pageIndex, pageSize);

            TableDatas<SysUser> table = new TableDatas<SysUser>();
            table.data = models;
            table.total = 20;

            return Json(table);
        }

    }
}
